package com.yonyou.jrmob.contacts.contact.activity;

/**
 * Created by 11985 on 2016/10/27.
 */
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yonyou.jrmob.contacts.R;
import com.yonyou.jrmob.contacts.contact.adapter.InterfacePropleAdapter_ct;
import com.yonyou.jrmob.contacts.contact.bean.InterfacePeopleVO_ct;
import com.yonyou.uap.um.service.ServiceCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import jrmob.yonyou.com.commonlib.baseui.base.BaseActivityMob_lib;
import jrmob.yonyou.com.commonlib.login.bean.MAServices_lib;
import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.CommonUtil;

/**
 * 接口人界面
 * Created by GaoHW on 2016/7/13.
 */
public class InterfacePeopleActivity_ct extends BaseActivityMob_lib implements OnClickListener{

    private RelativeLayout top_rl;
    private ImageView imgBack; // 返回
    private TextView tvTitle; // 标题
    //private Button btn_back;

    private List<InterfacePeopleVO_ct> data;
    private ListView listView;
    private InterfacePropleAdapter_ct adapter;

    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
    private static final int CODE_FOR_CALL_PHONE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initActionbar();
        setContentView(R.layout.activity_interface_people_ct);

        initUI();
        initData();
        initListener();

    }

    @Override
    public void initView() {

    }

    /**
     * @desc 初始化界面
     */
    private void initUI(){
        top_rl = (RelativeLayout) findViewById(R.id.common_top_rl);
        imgBack = (ImageView) findViewById(R.id.iv_top_back);
        imgBack.setVisibility(View.VISIBLE);
        tvTitle = (TextView) findViewById(R.id.top_tv_title);
        //btn_back = (Button) findViewById(R.id.btn_close);

        listView = (ListView) findViewById(R.id.lv_interface_people);
    }

    /**
     * @desc 初始化数据
     */
    @Override
    public void initData(){
        top_rl.setBackgroundColor(this.getResources().getColor(R.color.attendance_center_bg));
//        top_rl.setBackgroundColor(Color.parseColor("#36bc99"));
        tvTitle.setText("接口人");
//        btn_back.setText("");

        adapter = new InterfacePropleAdapter_ct(InterfacePeopleActivity_ct.this, null);
        listView.setAdapter(adapter);

        getData();
    }

    /**
     * @desc 调用接口，获取接口人数据
     * @author GaoHW
     * @time 2016/7/13 11:09
     */
    private void getData(){
        // TODO Auto-generated method stub
        //检查网络
        if (!CommonUtil.isNetworkConnected(this)) {
            CommonUtil.showToast(this, "当前网络不可用,请检查!");
            return;
        }
        JSONObject json = new JSONObject();

        try {
            json.put("servicetype", "ipa");
            json.put("transtype", "itfpsn");
            json.put("user_id", getUserInfo().getUserid());
            json.put("sessionkey", getUserInfo().getSessionkey());

            CommonUtil.openProgressDialog(this,"提示","正在刷新");

            MAServices_lib.callMACommonService(this, new ServiceCallback() {
                @Override
                public void execute(JSONObject jsonObject) {
                    CommonUtil.closeProgressDialog(InterfacePeopleActivity_ct.this);
                    try {

                        JSONObject json = (JSONObject) jsonObject.opt("result");
                        Object obj = json.opt("retflag");
                        if(obj != null && obj.toString().trim().equals("1")){
                            String msg = json.opt("msg").toString().trim();
                            showMessage(msg);

                            if(msg.contains("请重新登录")){
                                turnToLoginActivity(InterfacePeopleActivity_ct.this);
                            }
                            return;
                        }


                        JSONObject result =  jsonObject.getJSONObject("result");
                        if(result.getString("returncode").equals("0")){
                            adapter.clear();
                            data = parseInterfacePeople(result);
                            adapter.append(data);
                        }else{
                            CommonUtil.showToast(InterfacePeopleActivity_ct.this, result.getString("msg"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void error(String s, String s1) {
                    CommonUtil.closeProgressDialog(InterfacePeopleActivity_ct.this);
                    CommonUtil.showToast(InterfacePeopleActivity_ct.this, s + s1);
                }
            },json);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * @desc 解析接口人列表数据
     * @author GaoHW
     * @time 2016/7/13 11:17
     */
    public static List<InterfacePeopleVO_ct> parseInterfacePeople(JSONObject result)
            throws JSONException {
        List<InterfacePeopleVO_ct> data = new ArrayList<InterfacePeopleVO_ct>();
        JSONArray array = result.getJSONArray("data");
        for (int i = 0; i < array.length(); i++) {
            InterfacePeopleVO_ct vo = new InterfacePeopleVO_ct();
            vo.setName(array.getJSONObject(i).getString("user_name"));
            vo.setAdpt(array.getJSONObject(i).getString("dept_name"));
            vo.setTel(array.getJSONObject(i).getString("user_phone"));
            data.add(vo);
        }
        return data;
    }


    /**
     * @desc 监听
     */
    @Override
    public void initListener() {
        imgBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
